const { Success, Fail, Custom } = require("../../utils/status");
const moment = require("moment");
const { fileList } = require("../../service/filelist")
const { downloadFileListByIds, insertDownloadFile, downloadFileDeleteById, getDownloadFileById, downloadFileUpdateById } = require("../../service/downloadfile")
module.exports = {
    fileList: async (ctx) => {
        let { id, username } = ctx.state.user;
        let { oldFileName = "", pageNum, pageSize = 10 } = ctx.request.body;
        var send;
        if (pageNum < 1) {
            send = new Fail({
                msg: "pageNum要大于0"
            });
        } else {
            var { fileListData, total } = await fileList({
                id,
                pageNum,
                pageSize,
                oldFileName
            })
            var ids = fileListData.map(item => item.id);
            var downloadFileList = await downloadFileListByIds(ids);
            fileListData.forEach(element => {
                element.child = downloadFileList.filter(item => item.fileListId == element.id);
            });
            send = new Success({
                data: {
                    data: fileListData,
                    total
                },
                msg: "成功！"
            });
        }
        ctx.body = send
    },
    downloadFileAdd: async (ctx) => {
        let { id, username } = ctx.state.user;
        let { fileListId, downloadFileName = "", downloadSpeed, expirationTime } = ctx.request.body;
        var send;
        if (fileListId && expirationTime) {
            var data = await insertDownloadFile({
                userId: id,
                fileListId,
                downloadFileName,
                downloadSpeed: downloadSpeed,
                expirationTime: moment(expirationTime).format("YYYY-MM-DD HH:mm:ss"),
                createTime: moment().format("YYYY-MM-DD HH:mm:ss")
            })
            if (data) {
                send = new Success({
                    data: {},
                    msg: "添加成功！"
                });
            } else {
                send = new Fail({
                    msg: "添加失败！"
                });
            }
        } else {
            send = new Fail({
                msg: "参数错误！"
            });
        }
        ctx.body = send
    },
    deleteDownloadFile: async (ctx) => {
        let { id, username } = ctx.state.user;
        let { downloadFileId } = ctx.request.body;
        var send;
        if (downloadFileId) {
            var data = await getDownloadFileById(downloadFileId);
            if (data) {
                if (data.downloadSpeed == 0 && !data.expirationTime) {
                    send = new Fail({
                        msg: "永久不限速链接不能删除！"
                    });
                } else {
                    var deleteData = await downloadFileDeleteById({
                        id: downloadFileId,
                        userId: id,
                    });
                    send = new Success({
                        data: deleteData,
                        msg: "删除成功！"
                    });
                }
            } else {
                send = new Fail({
                    msg: "id无效"
                });
            }
        } else {
            send = new Fail({
                msg: "id无效"
            });
        }
        ctx.body = send
    },
    updateDonwloadFile: async (ctx) => {
        let { id, username } = ctx.state.user;
        let { downloadFileName, downloadSpeed, expirationTime, downloadFileId } = ctx.request.body;
        var send;
        if (downloadSpeed && expirationTime && downloadFileId) {
            var data = await downloadFileUpdateById({
                id: downloadFileId,
                userId: id,
                downloadSpeed,
                expirationTime: moment(expirationTime).format("YYYY-MM-DD HH:mm:ss"),
                downloadFileName
            })
            if (data) {
                send = new Success({
                    msg: "更新成功！"
                });
            } else {
                send = new Fail({
                    msg: "更新失败！"
                });
            }
        } else {
            send = new Fail({
                msg: "参数错误！"
            });
        }
        ctx.body = send
    }
}